# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from heroSpider.items import *
import json


def writeJson(item, file):
    with open(file, 'a+') as f:
        f.write(json.dumps(dict(item)) + ",\n")


class HerospiderPipeline:
    def process_item(self, item, spider):
        if isinstance(item, HeroItem):
            writeJson(item, 'hero.txt')
        elif isinstance(item, HeroStatsItem):
            writeJson(item, 'hero-stats.txt')
        elif isinstance(item, HeroSkillsItem):
            writeJson(item, 'hero-skills.txt')
        elif isinstance(item, SoulbindItem):
            writeJson(item, 'hero-soul-bind.txt')
        elif isinstance(item, RebonItem):
            writeJson(item, 'hero-reborn.txt')

        return item
